// 01login/reg/regJyhsb.js
var co = require('../../resource/js/common.js')
var Map = require("../../resource/js/map.js")
var app = getApp()
var pKeys = ['latitude', 'longitude']
var _this, _data
var _Sms_Code
//首页输入的手机， 是否已是商家     定时器
var _inputPhone, interval = null
//是否结束定时器
var isExitCountTime = 0
Page({
   data: {
      user: [], //用户信息
      Verified_Phone: '', //验证的手机号,初始为0，自动登录后变为非0，以确认要否显示微信授权
      getSmsCodeText: '获取验证码',
      Verified_Ok: '', //验证正确否
      isCode: 0, //验证码0错误，1正确
      isLogin: 1, //1表示未授权。0表示已授权 
      orgReg: false,
   },
   onLoad(opt) {
      _this = this
      _data = this.data
      //去自动登录
      _this.toAutoLogin()
   },
   onUnload() {
      isExitCountTime = 1
   },
   //一键获取微信认证手机
   getPhoneNumber(e) {
      app.decryptWxInfoData(e.detail, function(res) {
         _this.setData({
            Verified_Ok: 1,
            Verified_Phone: res.phoneNumber
         })
      })
   },
   //手机输入框失去焦点
   inputPhone(e) {
      _inputPhone = e.detail.value
      console.log('【inputPhone(e)】===' + _inputPhone)
   },
   //检查手机格式
   checkPhone(phone) {
      var pho = phone ? phone : _inputPhone
      var rule = /^1[0-9]{10}$/
      console.log('【checkPhone(phone)==】' + pho)
      if (rule.test(pho)) {
         return 1
      }
      return 0
   },
   //倒计时
   countDown(time) {
      var second = ''
      setTimeout(function() {
         if (time > 0) {
            time--
            second = '(' + time + '秒)'
            _this.setData({
               getSmsCodeText: '重新获取' + second
            })
            if (!isExitCountTime) {
               _this.countDown(time)
            }
         } else {
            isExitCountTime = 0
            _this.setData({
               getSmsCodeText: '重新获取' + second
            })
         }
      }, 1000);
   },
   // 输入验证码
   inputCode(e) {
      console.log('输入码==' + e.detail.value)
      if (_Sms_Code == e.detail.value) {
         // if ('232' == e.detail.value)
         isExitCountTime = 1
         this.setData({
            Verified_Ok: 1,
            Verified_Phone: _data.Verified_Phone
         })
      }
   },
   //换个手机
   changePhone() {
      this.setData({
         Verified_Ok: 0,
         Verified_Phone: ''
      })
   },
   // 获取短信验证码
   getPhoneCode() {
      if (!this.checkPhone()) {
         co.toastAllText('手机号格式不正确')
         return
      }
      this.setData({
         Verified_Phone: _inputPhone
      })
      co.getShareApi('api/Sms/getPhoneCode', {
         mobile: _inputPhone,
         type: 'user_reg',
         appid: "1400134010"
      }, function(res) {
         if (res.code == 0) {
            _Sms_Code = res.data.Sms_Code
            var second = 90
            _this.setData({
               getSmsCodeText: '重新获取(' + second + '秒)'
            })
            _this.countDown(second)
         } else {
            co.toastAllText(res.msg)
         }
      })
   },
   //点击授权
   getWxUserInfo(e) {
         var session_key = wx.getStorageSync('session_key')
         if (co.empty(session_key)) {
            app.wxLogin(function(res) {
               console.log("【app.wxLogin登录结果】===", res)
               _this.getUserInfo(e.detail)
            })
            return
         }
        _this.getUserInfo(e.detail)
   },
   //获取用户 或 注册 或更新
   getUserInfo(data) {
      if (co.empty(data.iv) || co.empty(data.encryptedData))
         return
      co.getShareApi('jyhs/Usermg/userRegister', {
         appid: app.Data.app.appid,
         session_key: wx.getStorageSync('session').session_key,
         iv: data.iv,
         encryptedData: data.encryptedData,
         User_Mobile: _this.data.Verified_Phone,
         User_Type: "jyhsb_check",
         Users_ID:"jyhs"
      }, function(res) {
         if (res.code != 0) {
            _this.setData({
               user: ''
            })
            co.toastAllText(res.msg)
            wx.navigateTo({
               url: '/01login/reg/regPerfect/regPerfect',
            })
            return
         } else {
            app.Data.userInfo = res.data
            _this.setData({
               user: res.data
            })
            wx.navigateTo({
               url: '/01login/reg/regPerfect/regPerfect',
            })
         }
      })
   },
   //提示手机认证
   showToast() {
      co.toastAllText('请先手机认证')
   },
   //准备自动登录
   toAutoLogin() {
      var session = wx.getStorageSync('session')
      if (!co.empty(session.unionid) || !co.empty(session.openid)) {
         _this.autoLogin(session)
         return
      }

      app.wxLogin(function(res) {
         _this.autoLogin(res)
      })
   },
   //自动登录
   autoLogin(obj) {
      co.getShareApi("jyhs/Usermg/autoLogin", {
         unionid: obj.unionid,
         openid: obj.openid, //因为换appid，需要更新Wx_Openid
         appid: app.Data.app.appid //因为换appid，需要更新Wx_Openid
      }, function(res) {
         if (res.code == 29300) {
            _this.doUserData([], '', '')
            return;
         }
         var d = res.data
         var biz_list = co.empty(d.biz_list) ? [] : d.biz_list
         _this.doUserData(biz_list, d.userInfo, d.userInfo.User_Mobile)
      })
   },
   //处理自动登录的用户数据
   doUserData(biz_list, userInfo, mobile) {
      app.Data.userInfo = userInfo
      if (biz_list.length)
         app.Data.shopInfo = biz_list[0]
      this.setData({
         user: userInfo,
         Verified_Ok: _this.checkPhone(mobile),
         Verified_Phone: mobile
      })
      console.log('_data====', _data)
   },
})